<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../Vue全家桶_stu/03_依赖包/vue.js"></script>
    <!-- v-for 用于渲染列表 可循环数组 对象 字符串 数字 -->
</head>

<body>
    <div id="root">
        <ul>
            <!-- v-for遍历 数组 -->
            <li v-for="(item,index) in list" :key="index">
                {{item.name}}
                <!-- v-for可嵌套使用 -->
                <p v-for="(childrenItem,indexItem) in item.children" :key="indexItem">
                    <!-- 点击a标签变为input 用自身的flag来控制谁显示谁隐藏 -->
                    <a href="#" @click="checkInput(childrenItem,indexItem)" v-show="childrenItem.flag">{{childrenItem.name}}</a>
                    <!-- input失焦后变为a标签 -->
                    <input type="text" style="width: 30px;" v-model="childrenItem.name" v-if="!childrenItem.flag"
                        @blur="checkA(childrenItem)"
                        @keydown.enter="$event.target.blur()" 
                         ref="InputNode">
                </p>

            </li>
        </ul>
    </div>
    <script>
        const vm = new Vue({
            el: '#root',
            data: {
                state: 'read',
                list: [
                    { name: '首页', children: [{ name: '添加' }, { name: '删除' }] },
                    { name: '添加商品', children: [{ name: '添加' }, { name: '删除' }] },
                    { name: 'spu管理', children: [{ name: '添加' }, { name: '删除' }] },
                    { name: 'sku管理', children: [{ name: '添加' }, { name: '删除' }] },
                ]
            },
            mounted() {
                //给list的每一个children的元素加一个flage
                this.list.map((item => {
                    item.children.map((childrenItem) => { this.$set(childrenItem, 'flag', true); return childrenItem })
                    return item
                }))
            },
            methods: {
                //点击a标签
                checkInput(item,index) {
                    //修改显示
                    item.flag = !item.flag
                    this.$nextTick(()=>{
                        //input获取焦点
                        this.$refs.InputNode[0].focus()
                    })
                },
                //input失焦
                checkA(item) {
                    //修改显示
                    item.flag = !item.flag
                }
            }
        })
    </script>
</body>

</html>